﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PresentationLayer.Views
{
    using System.Windows.Forms;
    using WinFormsMvp.Forms;
    using WinFormsMvp;
    using PresentationLayer.Presenters;
    using EngineLayer;
    using PresentationLayer.Properties;
    using PresentationLayer.Views.ViewContracts;
    using System.ComponentModel;
    using EngineLayer.Services;
    using System.Data;
    using EngineLayer.Model;

    [PresenterBinding(typeof(ProductsCatalogsPresenter))]
    public class ProductsCatalogsForm : MvpForm<MyApplication>, ICommonCatalogView
    { 
        #region Private Declarations 
          private IEnumerable<object> _DataSource = null;
          private Panel panelGlobal;
          private Button btnSave;
          private Button btnCancel;
          private Panel panelTop;
          private GroupBox groupBox3;
          private Button btnUpdate;
          private GroupBox groupBox2;
          private Button btnAdd;
          private GroupBox groupBox1;
          private TextBox txtFind;
          private Panel panelGrid;
          private Panel panelBottom;
        //GRID DATA
          private DataGridView dataGridView;
          private DataGridViewTextBoxColumn Codigo;
          private DataGridViewTextBoxColumn DescripciónCorta;
          private DataGridViewTextBoxColumn DescripciónLarga;
          private DataGridViewTextBoxColumn Precio;
          private DataGridViewComboBoxColumn Categoria;
          private DataGridViewTextBoxColumn ProductCategory;
          private DataGridViewTextBoxColumn Product;
          private DataGridViewCheckBoxColumn Public;
        #endregion

       protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.InitializeComponent();

            GetComboGridData(null, EventArgs.Empty); 

            GetCatalogData(null, EventArgs.Empty);
        }
        
       private void InitializeComponent()
       {
           this.btnSave = new Button();
           this.btnCancel = new Button();

           this.panelTop = new Panel();
           this.groupBox3 = new GroupBox();
           this.btnUpdate = new Button();
           this.groupBox2 = new GroupBox();
           this.btnAdd = new Button();
           this.groupBox1 = new GroupBox();
           this.txtFind = new TextBox();
           this.panelGrid = new Panel();
           this.panelGlobal = new Panel();
           this.panelBottom = new Panel();

           ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ProductsCatalogsForm));
           DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();
           DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();
           DataGridViewCellStyle dataGridViewCellStyle3 = new DataGridViewCellStyle();
           DataGridViewCellStyle dataGridViewCellStyle4 = new DataGridViewCellStyle();
           DataGridViewCellStyle dataGridViewCellStyle5 = new DataGridViewCellStyle();
           DataGridViewCellStyle dataGridViewCellStyle6 = new DataGridViewCellStyle();

           this.dataGridView = new DataGridView();
           this.Codigo = new DataGridViewTextBoxColumn();
           this.DescripciónCorta = new DataGridViewTextBoxColumn();
           this.DescripciónLarga = new DataGridViewTextBoxColumn();
           this.Precio = new DataGridViewTextBoxColumn();
           this.Categoria = new DataGridViewComboBoxColumn();
           this.ProductCategory = new DataGridViewTextBoxColumn();
           this.Product = new DataGridViewTextBoxColumn();
           this.Public = new DataGridViewCheckBoxColumn();
           ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();

           this.dataGridView.SuspendLayout();
           this.panelBottom.SuspendLayout();
           this.panelGrid.SuspendLayout();
           this.panelTop.SuspendLayout();
           this.groupBox3.SuspendLayout();
           this.groupBox2.SuspendLayout();
           this.groupBox1.SuspendLayout();
           this.panelGlobal.SuspendLayout();
           this.panelBottom.SuspendLayout();
           this.panelGrid.SuspendLayout();
           
        
           this.SuspendLayout();  
                // 
                // panelGlobal
                // 
                //  this.panelGlobal = (Activator.CreateInstance(typeof(CommonCatalogControl)) as MvpUserControl<CatalogsModel>);
                this.panelGlobal.Controls.Add(this.dataGridView);
                this.panelGlobal.Dock = DockStyle.Fill;
                this.panelGlobal.Location = new System.Drawing.Point(0, 479);
                this.panelGlobal.Name = "panelGlobal";
                this.panelGlobal.Size = new System.Drawing.Size(875, 51);
                this.panelGlobal.TabIndex = 21;
                // 
                // btnSave
                // 
                this.btnSave.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
                this.btnSave.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnSave.Location = new System.Drawing.Point(678, 16);
                this.btnSave.Name = "btnSave";
                this.btnSave.Size = new System.Drawing.Size(80, 25);
                this.btnSave.TabIndex = 2;
                this.btnSave.Text = Resources.Guardar;
                this.btnSave.Tag = Resources.Guardar;
                this.btnSave.UseVisualStyleBackColor = true;
                this.btnSave.Click += new System.EventHandler(this.OnClickButton);
                // 
                // btnCancel
                // 
                this.btnCancel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
                this.btnCancel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.btnCancel.Location = new System.Drawing.Point(764, 15);
                this.btnCancel.Name = "btnCancel";
                this.btnCancel.Size = new System.Drawing.Size(83, 26);
                this.btnCancel.TabIndex = 3;
                this.btnCancel.Text = Resources.Cancelar;
                this.btnCancel.Tag = Resources.Cancelar;
                this.btnCancel.UseVisualStyleBackColor = true;
                this.btnCancel.Click += new System.EventHandler(this.OnClickButton);
                // 
                // panelCommon
                // 
                this.panelTop.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
                this.panelTop.Controls.Add(this.groupBox3);
                this.panelTop.Controls.Add(this.groupBox2);
                this.panelTop.Controls.Add(this.groupBox1);
                this.panelTop.Dock = DockStyle.Top;
                this.panelTop.Location = new System.Drawing.Point(0, 0);
                this.panelTop.Name = "panelTop";
                this.panelTop.Size = new System.Drawing.Size(875, 116);
                this.panelTop.TabIndex = 19;
                // 
                // groupBox3
                // 
                this.groupBox3.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
                this.groupBox3.Controls.Add(this.btnUpdate);
                this.groupBox3.Location = new System.Drawing.Point(734, 14);
                this.groupBox3.Name = "groupBox3";
                this.groupBox3.Size = new System.Drawing.Size(113, 89);
                this.groupBox3.TabIndex = 21;
                this.groupBox3.TabStop = false;
                this.groupBox3.Text = "Actualizar en Linea";
                // 
                // btnUpdate
                // 
                this.btnUpdate.Anchor = AnchorStyles.None;
                this.btnUpdate.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)), true);
                this.btnUpdate.ForeColor = System.Drawing.Color.Transparent;
                this.btnUpdate.Image = Resources.NetworkUpdate; 
                this.btnUpdate.Location = new System.Drawing.Point(17, 14);
                this.btnUpdate.Name = "btnUpdate";
                this.btnUpdate.Tag = Resources.Actualizar;
                this.btnUpdate.Size = new System.Drawing.Size(79, 61);
                this.btnUpdate.TabIndex = 18;
                this.btnUpdate.UseVisualStyleBackColor = true;
                this.btnUpdate.Click += new System.EventHandler(this.OnClickButton);
                // 
                // groupBox2
                // 
                this.groupBox2.Controls.Add(this.btnAdd);
                this.groupBox2.Location = new System.Drawing.Point(475, 14);
                this.groupBox2.Name = "groupBox2";
                this.groupBox2.Size = new System.Drawing.Size(62, 89);
                this.groupBox2.TabIndex = 20;
                this.groupBox2.TabStop = false;
                this.groupBox2.Text = Resources.Agregar;
                // 
                // btnAdd
                // 
                this.btnAdd.Anchor = AnchorStyles.None;
                this.btnAdd.Image = Resources.PlusSymbol;
                this.btnAdd.ForeColor = System.Drawing.Color.Transparent;   
                this.btnAdd.Location = new System.Drawing.Point(11, 28);
                this.btnAdd.Name = "btnAdd";
                this.btnAdd.Tag = Resources.Agregar;
                this.btnAdd.Size = new System.Drawing.Size(39, 43);
                this.btnAdd.TabIndex = 17;
                this.btnAdd.UseVisualStyleBackColor = true;
                this.btnAdd.Click += new System.EventHandler(this.OnClickButton);
                // 
                // groupBox1
                // 
                this.groupBox1.Controls.Add(this.txtFind);
                this.groupBox1.Location = new System.Drawing.Point(12, 14);
                this.groupBox1.Name = "groupBox1";
                this.groupBox1.Size = new System.Drawing.Size(457, 89);
                this.groupBox1.TabIndex = 19;
                this.groupBox1.TabStop = false;
                this.groupBox1.Text = Resources.Busqueda;
                // 
                // textBoxFind
                // 
                this.txtFind.Location = new System.Drawing.Point(16, 37);
                this.txtFind.Name = "txtFind";
                this.txtFind.Size = new System.Drawing.Size(419, 20);
                this.txtFind.TabIndex = 0;
                this.txtFind.TextChanged += new System.EventHandler(this.TextBoxFindChanged);
                // 
                // panelBottom
                this.panelBottom.Controls.Add(this.btnSave);
                this.panelBottom.Controls.Add(this.btnCancel);
                this.panelBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
                this.panelBottom.Location = new System.Drawing.Point(0, 479);
                this.panelBottom.Name = "panelBottom";
                this.panelBottom.Size = new System.Drawing.Size(875, 51);
                this.panelBottom.TabIndex = 21;
                  // 
                // dataGridView1
                // 
                this.dataGridView.Dock = DockStyle.Fill;
                this.dataGridView.AllowUserToOrderColumns = true;
                this.dataGridView.AllowUserToAddRows = true;
                this.dataGridView.AllowUserToDeleteRows = true;
                this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)));
                this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.Codigo,
                this.DescripciónCorta,
                this.DescripciónLarga,
                this.Precio,
                this.Categoria, 
                this.Product,
                this.ProductCategory,
                this.Public});
                this.dataGridView.Location = new System.Drawing.Point(0, 20);
                this.dataGridView.Name = "dataGridView";
                this.dataGridView.Size = new System.Drawing.Size(844, 325);
                this.dataGridView.TabIndex = 9;
                this.dataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(DataGridViewError);
                this.dataGridView.CellContentClick += new DataGridViewCellEventHandler(DataGridViewCellContentClick);
                // 
                // Codigo
                // 
                this.Codigo.DataPropertyName = "ID";
                dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
                dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
                this.Codigo.DefaultCellStyle = dataGridViewCellStyle1;
                this.Codigo.HeaderText = "Código";
                this.Codigo.Name = "Codigo";
                // 
                // DescripciónCorta
                // 
                this.DescripciónCorta.DataPropertyName = "Name";
                dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
                this.DescripciónCorta.DefaultCellStyle = dataGridViewCellStyle2;
                this.DescripciónCorta.HeaderText = "Descripción Corta";
                this.DescripciónCorta.Name = "DescripciónCorta";
                this.DescripciónCorta.Width = 150;
                // 
                // DescripciónLarga
                // 
                this.DescripciónLarga.DataPropertyName = "Description";
                dataGridViewCellStyle3.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
                this.DescripciónLarga.DefaultCellStyle = dataGridViewCellStyle3;
                this.DescripciónLarga.HeaderText = "Descripción Larga";
                this.DescripciónLarga.Name = "DescripciónLarga";
                this.DescripciónLarga.Width = 250;
                // 
                // Precio
                // 
                this.Precio.DataPropertyName = "Price";
                dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
                dataGridViewCellStyle4.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
                dataGridViewCellStyle4.Format = "C2";
                dataGridViewCellStyle4.NullValue = null;
                this.Precio.DefaultCellStyle = dataGridViewCellStyle4;
                this.Precio.HeaderText = "Precio";
                this.Precio.Name = "Precio";
                // 
                // Categoria
                // 
                this.Categoria.DataPropertyName = "CategoryID";
                dataGridViewCellStyle5.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
                this.Categoria.DefaultCellStyle = dataGridViewCellStyle5;
                this.Categoria.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
                this.Categoria.HeaderText = "Categoria";
                this.Categoria.Name = "Categoria"; 
                this.Categoria.Resizable = System.Windows.Forms.DataGridViewTriState.True;
                this.Categoria.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
                this.Categoria.Width = 130;
                // 
                // ProductCategory
                // 
                this.ProductCategory.DataPropertyName = "Category";
                dataGridViewCellStyle6.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
                this.ProductCategory.DefaultCellStyle = dataGridViewCellStyle6;
                this.ProductCategory.HeaderText = "Category";
                this.ProductCategory.Name = "Category";
                this.ProductCategory.Visible = false;
                // 
                // Product
                // 
                this.Product.DataPropertyName = "product";
                dataGridViewCellStyle6.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
                this.Product.DefaultCellStyle = dataGridViewCellStyle6;
                this.Product.HeaderText = "product";
                this.Product.Name = "product";
                this.Product.Visible = false;
                // 
                // Public
                // 
                this.Public.DataPropertyName = "Public";
                this.Public.HeaderText = "Público";
                this.Public.Name = "Public";
                this.Public.Resizable = System.Windows.Forms.DataGridViewTriState.True;
                this.Public.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
                this.Public.Width = 50;
                // 
                // panelGrid
                this.panelGrid.Controls.Add(this.dataGridView);
                this.panelGrid.Dock = System.Windows.Forms.DockStyle.Fill;
                this.panelGrid.Location = new System.Drawing.Point(0, 116);
                this.panelGrid.Name = "panelGrid";
                this.panelGrid.Size = new System.Drawing.Size(875, 414);
                this.panelGrid.TabIndex = 22;
                 
                // 
                // ProductForm
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(875, 530);
                this.Controls.Add(this.panelGrid);
                this.Controls.Add(this.panelTop);
                this.Controls.Add(this.panelBottom);


                this.Name = "ProductsCatalogsForm";
                this.Text = "";
                this.dataGridView.ResumeLayout(false);
                this.panelTop.ResumeLayout(false);
                this.panelBottom.ResumeLayout(false);
                this.panelGrid.ResumeLayout(false);
                this.panelTop.ResumeLayout(false);
                this.groupBox3.ResumeLayout(false);
                this.groupBox2.ResumeLayout(false);
                this.groupBox1.ResumeLayout(false);
                this.groupBox1.PerformLayout();
                this.panelGrid.ResumeLayout(false);

                //FORM stuffs

                this.Controls.Add(panelGlobal);
                this.Controls.Add(panelTop);
                this.Controls.Add(panelBottom);
                this.FormBorderStyle = FormBorderStyle.FixedSingle;
                this.WindowState = FormWindowState.Maximized;
                this.FormBorderStyle = FormBorderStyle.FixedSingle;
                this.ResumeLayout(false);
                this.PerformLayout();
            } 

       private void RefreshGridData()
       {
           BindingSource bindingSource = new BindingSource();
           bindingSource.DataSource = _DataSource;
           dataGridView.DataSource = bindingSource;
       }

       #region Implementation of IProductsCatalogsView
 
       public event EventHandler GetComboGridData;
       public event EventHandler GetCatalogData;
       public event EventHandler TextBoxFindChanged;
       public event EventHandler OnClickButton;
       public event DataGridViewCellEventHandler DataGridViewCellContentClick;

       public event DataGridViewDataErrorEventHandler DataGridViewError;

       public void Binding(IEnumerable<object> DataSource)
        {
            _DataSource = DataSource;
            RefreshGridData();
        }

       public void ComboGridInfo(IEnumerable<object> DataSource)
       {
           this.Categoria.DataSource = DataSource;
           this.Categoria.DisplayMember = "Name";
           this.Categoria.ValueMember = "Id";
       }

       public void DataGridErrors(object sender, DataGridViewDataErrorEventArgs Err)
        {
            try
            {
            }
            catch 
            { }
        
        }
      public void CloseForm()
        {
            this.Close();
        }
 
        #endregion
    }
}
  
 
